package com.wyx.easyExcel.easyExcelListen;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.wyx.easyExcel.easyExcelPojo.putTactics.AnticipatedOrderData;
import com.wyx.easyExcel.easyExcelPojo.putTactics.GearExtendSelection;
import com.wyx.easyExcel.easyExcelPojo.putTactics.PriceSelection;
import com.wyx.easyExcel.easyExcelPojo.putTactics.WeeklySupplyStrategyData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// 周货源投放策略的监听
public class WeeklySupplyStrategyDataListener extends AnalysisEventListener<Map<Integer, String>> {
    private WeeklySupplyStrategyData weeklySupplyStrategyData;
    //当前投放方式，会被修改，
    private String currentDeliveryMode;
    //价格自选投放 有多个组合，从1开始,当读取到 "价位段订购上限" 时, id进行+1
    private int priceSelectPutId = 1;
    // 当前档位扩展投放的商品编码
    private String currentGearExtendSupplyCode;
    private int gearExtendSupplySize;
    // 是否读取结束，如果读取到投放规则就结束
    private boolean isOver = false;
    // 对于单元格的合并问题，缓存第一条完整的数据
    private AnticipatedOrderData preAnticipatedOrderData;

    public WeeklySupplyStrategyDataListener(WeeklySupplyStrategyData weeklySupplyStrategyData) {
        this.weeklySupplyStrategyData = weeklySupplyStrategyData;
    }

    @Override
    public void invoke(Map<Integer, String> integerStringMap, AnalysisContext analysisContext) {
        analyze(integerStringMap, analysisContext);
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    }

    //最后这个数据写成一个内部类，如果没有其他地方使用这个类的话。
    //  解析这个map数据。  // 文件名称   文件名称应该在哪里，反正不是在这里
    public void analyze(Map<Integer, String> data, AnalysisContext context) {
        if (isOver) {
            return;
        }
        Map<String, String> noteInfo = weeklySupplyStrategyData.getNoteInformationMap();
        if (data.get(0) == null && data.get(1) == null && data.get(2) == null) {
            return;
        }
        String firstIndexData = null;
        if (data.get(0) != null) {
            firstIndexData = data.get(0).trim();
        } else {
            firstIndexData = "";
        }
        if ("周货源投放策略".equals(firstIndexData)) {
            noteInfo.put("title", firstIndexData);
            return;
        }
        Pattern pattern = Pattern.compile("公司:(.*?) ; 周选择:(.*?);");
        Matcher matcher = pattern.matcher(firstIndexData);

        if (matcher.find()) {
            // 提取公司名称
            noteInfo.put("companyName", matcher.group(1).trim());
            // 提取日期范围
            noteInfo.put("dateRange", matcher.group(2).trim());
            return;
        }

        if (firstIndexData.startsWith("投放方式:")) {
            currentDeliveryMode = firstIndexData.split(":")[1].trim();
            return;
        }
        if (firstIndexData.equals("商品编码")) {
            return;
        }
        if (firstIndexData.equals("按档位投放合计")) {
            addGear(data, weeklySupplyStrategyData.getGearSelection().getGearPutUpperLimit());
            return;
        }
        if (firstIndexData.matches("\\d+")||firstIndexData.equals("")) {
            AnticipatedOrderData baseData = getData(data, firstIndexData);
            switch (currentDeliveryMode) {
                case "按档位投放":
                    List<AnticipatedOrderData> gearSelectionList = weeklySupplyStrategyData.getGearSelection().getList();
                    gearSelectionList.add(baseData);
                    break;
                case "价位段自选投放":
                    List<PriceSelection> priceSelection = weeklySupplyStrategyData.getPriceSelection();
                    // 如果 集合中的数量小于当前组的id序号
                    if (priceSelection.size() < priceSelectPutId) {
                        priceSelection.add(new PriceSelection());
                    }
                    if (data.get(5) == null) {
                        baseData.setPriceBand(preAnticipatedOrderData.getPriceBand());
                    }
                    priceSelection.get(priceSelectPutId - 1).getList().add(baseData);
                    preAnticipatedOrderData = baseData;
                    break;
                case "档位扩展投放":
                    //首先判断是否是第一次，如果是第一次进行创建。
                    List<GearExtendSelection> gearExtendSelectionList = weeklySupplyStrategyData.getGearExtendSelectionList();
                    if (currentGearExtendSupplyCode == null) {
                        // 说明是第一次
                        GearExtendSelection gearExtendSelection = new GearExtendSelection();
                        gearExtendSelection.getList().add(baseData);
                        gearExtendSelectionList.add(gearExtendSelection);
                        currentGearExtendSupplyCode = firstIndexData;
                        gearExtendSupplySize++;
                    }else{
                        // 可能是新的集合也可能是旧的集合，如果是新的集合，则进行差创建，如果不是新的集合，就进行添加
                        if (firstIndexData.equals("")){
                            GearExtendSelection gearExtendSelection = gearExtendSelectionList.get(gearExtendSupplySize-1);
                            // 说明的确是旧的添加
                            AnticipatedOrderData currentAnticipatedOrderData = new AnticipatedOrderData();
                            currentAnticipatedOrderData.setProductEncoding(preAnticipatedOrderData.getProductEncoding());
                            currentAnticipatedOrderData.setProductName(preAnticipatedOrderData.getProductName());
                            currentAnticipatedOrderData.setReleaseOrNo(preAnticipatedOrderData.getReleaseOrNo());
                            currentAnticipatedOrderData.setBrandSpecification(preAnticipatedOrderData.getBrandSpecification());
                            currentAnticipatedOrderData.setPriceBand(preAnticipatedOrderData.getPriceBand());
                            currentAnticipatedOrderData.setAbnormal(preAnticipatedOrderData.getAbnormal());
                            currentAnticipatedOrderData.setSupplyTotal(preAnticipatedOrderData.getSupplyTotal());
                            currentAnticipatedOrderData.setType(baseData.getType());
                            currentAnticipatedOrderData.copyLevel(baseData);
                            gearExtendSelection.getList().add(currentAnticipatedOrderData);
                        }else if (!firstIndexData.equals(currentGearExtendSupplyCode)){
                            // 说明是新的集合添加
                            GearExtendSelection gearExtendSelection = new GearExtendSelection();
                            gearExtendSelection.getList().add(baseData);
                            gearExtendSelectionList.add(gearExtendSelection);
                            currentGearExtendSupplyCode = firstIndexData;
                            gearExtendSupplySize++;
                        }
                    }
                    preAnticipatedOrderData = baseData;
                    break;
            }
            return;
        }
        if (firstIndexData.equals("价位段订购上限")) {
            // 将价格自选投放添加进去
            List<Integer> gearPutUpperLimit = weeklySupplyStrategyData.getPriceSelection().get(priceSelectPutId - 1).getGearPutUpperLimit();
            addGear(data, gearPutUpperLimit);
            priceSelectPutId++;
            return;
        }
        if (firstIndexData.startsWith("投放规则")) {
            noteInfo.put("putRule", firstIndexData);
            isOver = true;
        }
    }

    private void addGear(Map<Integer, String> data, List<Integer> gearPutUpperLimit) {
        for (int i = 8; i <= 37; i++) {
            gearPutUpperLimit.add(Integer.parseInt(data.get(i)));
        }
    }

    // 解析数据 得到对应的bean
    public AnticipatedOrderData getData(Map<Integer, String> data, String firstIndexData) {
        AnticipatedOrderData baseData = new AnticipatedOrderData();
        baseData.setProductEncoding(firstIndexData);
        baseData.setProductName(data.get(1));
        baseData.setType(data.get(2));
        baseData.setReleaseOrNo(data.get(3));
        baseData.setBrandSpecification(data.get(4));
        baseData.setPriceBand(data.get(5));
        baseData.setAbnormal(data.get(6));
        baseData.setSupplyTotal(data.get(7));
        List<String> lvList = new ArrayList<>();
        for (int i = 8; i <= 37; i++) {
            lvList.add(data.get(i));
        }
        baseData.copyLevel(lvList);
        return baseData;
    }
}
